package com.k7k7.db;

import com.k7k7.db.component.UserWear;
import com.k7k7.db.entity.*;
import com.k7k7.db.relation.*;
import com.k7k7.db.util.HibernateSessionFactory;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-4-21
 * Time: 10:04:20
 */
public class UserManager {
	private static UserManager instance;

	public static UserManager getInstance() {
		if (instance == null) {
			instance = new UserManager();
		}
		return instance;
	}

	static Logger log = Logger.getLogger(UserManager.class);

	public static void main(String[] args) {
		UserManager.getInstance().newUser("wwj", "111111", true);
		log.info(UserManager.getInstance().login("wwj", "111111").getUserBase().getNick());
		//		mgr.createDefaultUser(null);
		//		mgr.testWear();
		//		mgr.testHouseFurniture();
		//		mgr.createFriends();
		//		mgr.listFriends();
		//mgr.testHouse();
		//mgr.testDress();
		//		HibernateSessionFactory.getSession().close();
		//		mgr.listUsers();
	}

	private void listFriends() {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		//
		User u1 = (User) session.load(User.class, 1l);
		Set<UserUser> uus = u1.getUserUsers();
		for (UserUser uu : uus) {
		}
		session.getTransaction().commit();
	}

	private void listUsers() {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		//
		User u2 = (User) session.load(User.class, 1l);
		for (UserHouse userHouse : u2.getHouses()) {
			for (UserHouseFurniture userHouseFurniture : userHouse.getFurniture()) {
			}
		}
		session.getTransaction().commit();
		HibernateSessionFactory.getSession().close();
	}

	private void testHouse() {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		//		
		User u1 = new User();
		u1.getUserWear().setHat("1");
		session.save(u1);
		//
		House house = new House();
		house.setMaterial(1);
		session.save(house);
		//adf
		UserHouse uhouse = new UserHouse();
		uhouse.setHouse(house);
		uhouse.setUser(u1);
		uhouse.setViewAmount(10l);
		session.save(uhouse);
		//
		u1.getHouses().add(uhouse);
		UserHouseFurniture fni = new UserHouseFurniture();
		fni.setX(10f);
		session.save(fni);
		//
		uhouse.getFurniture().add(fni);
		session.getTransaction().commit();
	}

	private void testDress() {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		//
		User u1 = new User();
		u1.getUserWear().setHat("1");
		session.save(u1);
		//
		Dress dress = new Dress();
		dress.setBuyPrice(100f);
		session.save(dress);
		//adf
		UserDress ud = new UserDress();
		ud.setUser(u1);
		ud.setFacility(dress);
		ud.setStartDate(new Date());
		session.save(ud);
		u1.getDresses().add(ud);
		////
		Prop prop = new Prop();
		prop.setBuyPrice(100f);
		session.save(prop);
		//adf
		UserProp up = new UserProp();
		up.setUser(u1);
		up.setFacility(prop);
		up.setStartDate(new Date());
		session.save(up);
		u1.getProps().add(up);
		////
		Goods goods = new Goods();
		goods.setBuyPrice(100f);
		session.save(goods);
		//adf
		UserGoods ug = new UserGoods();
		ug.setUser(u1);
		ug.setFacility(goods);
		ug.setStartDate(new Date());
		session.save(ug);
		u1.getGoods().add(ug);
		//
		session.getTransaction().commit();
	}

	private void createFriends() {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		User u1 = new User();
		session.save(u1);
		User u2 = new User();
		session.save(u2);
		//
		UserUser f1 = new UserUser();
		f1.setType(1);
		f1.setFriend(u2);
		u1.getUserUsers().add(f1);
		//
		session.getTransaction().commit();
	}

	private void testHouseFurniture() {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		User u = new User();
		//
		UserWear w = new UserWear();
		w.setUser(u);
		session.save(w);
		//
		UserHouseFurniture f = new UserHouseFurniture();
		f.setX(10f);
		f.setY(10f);
		session.save(f);
		//
		House house = new House();
		//		house.getFurniture().add(f);
		session.save(house);
		//
		session.save(u);
		session.getTransaction().commit();
	}

	private void testWear() {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		User u = new User();
		UserWear w = new UserWear();
		w.setUser(u);
		session.save(w);
		session.save(u);
		session.getTransaction().commit();
	}

	public User newUser(String name, String password, Boolean sex) {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		User u = new User();
		u.getUserBase().setNick(name);
		u.getUserBase().setPassword(password);
		u.getUserBase().setSex(sex);
		session.save(u);
		session.getTransaction().commit();
		HibernateSessionFactory.getSession().close();
		return u;
	}

	public User login(String name, String pwd) {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("from com.k7k7.db.entity.User u where u.userBase.nick=? and u.userBase.password=?");
		query.setParameter(0, name);
		query.setParameter(1, pwd);
		List list = query.list();
		HibernateSessionFactory.getSession().close();
		if (list.size() > 0) {
			return (User) list.get(0);
		}
		return null;
	}
}